﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace EmptyWallGallery.Core.BLL
{
    public class AlbumListView
    {
        public int AlbumId { get; set; }
        public string AlbumName { get; set; }
        public string AlbumDesc { get; set; }
        public DateTime CreateDate { get; set; }
        public string ImageDefault { get; set; }
        public int ViewCount { get; set; }
        public int SiteId { get; set; }
    }

    public class Album
    {
        public static Album Instance
        {
            get { return new Album(); }
        }

        public void UpdateViewCount(int albumId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                dc.Justin_Albums_UpdateViewCount(albumId);
            }
        }

        public static AlbumListView LoadDataFromListItem(tbl_Album item)
        {
            var objAlbum = new AlbumListView
            {
                AlbumId = item.AlbumID,
                AlbumName = item.AlbumName,
                AlbumDesc = item.AlbumDesc,
                CreateDate = DateTime.Parse(item.CreateDate.ToString()),
                ImageDefault = item.ImageDefault,
                ViewCount = Convert.ToInt32(item.ViewCount),
                SiteId = Convert.ToInt32(item.SiteId)
            };
            return objAlbum;
        }

        public bool Create(string albumName, string albumDesc, string imageDefault)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAlbum = new tbl_Album
                    {
                        AlbumName = albumName,
                        AlbumDesc = albumDesc,
                        ImageDefault = imageDefault,
                        CreateDate = DateTime.Now,
                        ViewCount = 0,
                        SiteId = Configs.SiteId
                    };

                    dc.tbl_Albums.InsertOnSubmit(objAlbum);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(string albumName, string albumDesc, string imageDefault, int albumId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAlbum = (dc.tbl_Albums.Where(p => p.AlbumID == albumId)).Single();
                    objAlbum.AlbumDesc = albumDesc;
                    objAlbum.AlbumName = albumName;
                    objAlbum.ImageDefault = imageDefault;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(int albumId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAlbum = dc.tbl_Albums.Where(p => p.AlbumID == albumId);

                    dc.tbl_Albums.DeleteAllOnSubmit(objAlbum);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public AlbumListView GetById(int albumId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var objAlbum = new AlbumListView();
                var query = (from p in dc.tbl_Albums where p.AlbumID == albumId select p).SingleOrDefault();

                if (query != null)
                {
                    objAlbum.AlbumDesc = query.AlbumDesc;
                    objAlbum.AlbumId = query.AlbumID;
                    objAlbum.AlbumName = query.AlbumName;
                    objAlbum.CreateDate = Convert.ToDateTime(query.CreateDate.ToString());
                    objAlbum.ImageDefault = query.ImageDefault;
                    objAlbum.ViewCount = Convert.ToInt32(query.ViewCount.ToString());
                    objAlbum.SiteId = Convert.ToInt32(query.SiteId);
                }
                else
                {
                    return null;
                }
                return objAlbum;
            }
        }

        public List<AlbumListView> GetAll()
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var queryable = from album in dc.tbl_Albums
                                where album.SiteId == Configs.SiteId
                                orderby album.AlbumID descending
                                select new AlbumListView
                                {
                                    AlbumDesc = album.AlbumDesc,
                                    AlbumName = album.AlbumName,
                                    AlbumId = album.AlbumID,
                                    CreateDate = Convert.ToDateTime(album.CreateDate),
                                    ImageDefault = album.ImageDefault,
                                    ViewCount = Convert.ToInt32(album.ViewCount),
                                    SiteId = Convert.ToInt32(album.SiteId)
                                };
                return queryable.ToList();
            }
        }
    }
}